from selenium import webdriver
from selenium.webdriver import ActionChains
from tools.logger import get_logger
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from selenium.webdriver.chrome.webdriver import WebDriver
from constant import REMOTE,IMPLICITLY_WAIT


class Driver:
    __driver = None
    __logger = get_logger()

    @classmethod
    def get_driver(cls) -> WebDriver:
        """
        获取驱动
        """
        if cls.__driver is None:
            if REMOTE is None:
                cls.__driver = webdriver.Chrome()
            else:
                cls.__driver = webdriver.Remote(REMOTE+'/wd/hub', desired_capabilities=DesiredCapabilities.CHROME)

            cls.__driver.implicitly_wait(IMPLICITLY_WAIT)
            cls.__driver.maximize_window()
            cls.__logger.info("连接打开应用成功")
        else:
            cls.__logger.info("连接打开应用失败")
        return cls.__driver

    @classmethod
    def quit_driver(cls, driver: WebDriver):
        if driver is not None:
            driver.quit()
        cls.__logger.info("关闭对象成功")

    @classmethod
    def action_driver(cls, driver: WebDriver) -> ActionChains:
        return ActionChains(driver)
